package com.health.auth.controller;

//
//import cn.dev33.satoken.annotation.SaCheckLogin;
//import cn.dev33.satoken.annotation.SaCheckRole;
//import cn.dev33.satoken.stp.StpUtil;
//import cn.dev33.satoken.util.SaResult;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.health.auth.model.UserLoginRequest;
import com.health.auth.service.LoginService;
import com.health.common.auth.service.TokenService;
import com.health.common.common.BaseResponse;
import com.health.common.common.ErrorCode;
import com.health.common.common.ResultUtils;
import com.health.common.exception.BusinessException;
import com.health.common.model.user.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhexueqi
 * @ClassName TokenController
 * @since 2024/8/19    18:56
 */
@RestController
@Tag(name = "TokenController")
@RequestMapping(value = "/auth")
public class TokenController {

    @Resource
    private TokenService tokenService;

    @Resource
    private LoginService loginService;


    @RequestMapping("checkLogin")
    @SaCheckLogin
    @SaCheckRole("super-admin")
    public SaResult checkLogin() {
        List<String> roleList = StpUtil.getRoleList();
        System.out.println(roleList);
//        StpUtil.checkRole("admin");
        // 获取当前会话的登录信息
        StpUtil.checkLogin();
        // 获取当前会话的登录id
        Object loginId = StpUtil.getLoginId();
        // 获取当前会话的登录设备
        String loginDevice = StpUtil.getLoginDevice();
        // 获取当前会话的token信息
        String tokenValue = StpUtil.getTokenValue();
        // 获取当前会话的token名称
        String tokenName = StpUtil.getTokenName();
        //获取当前会话的用户类型
        String loginType = StpUtil.getLoginType();
        return SaResult.data(StringUtils.join("当前会话的登录信息：", loginId, "当前会话的登录设备：", loginDevice, "当前会话的token名称：",
                tokenName, "当前会话的角色集合：", roleList, "当前会话的用户类型：", loginType, "当前会话的token信息：", tokenValue));
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public BaseResponse<?> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        Integer userRole = userLoginRequest.getUserRole();
        LoginUser loginUser = loginService.userLogin(userAccount, userPassword, userRole, request);
        return ResultUtils.success(tokenService.createToken(loginUser));
    }
}
